<?php
/*
 *广告类型管理
 *author 王建 
 */

class AdtypeAction extends BaseAction{
    public $d = '';
    function __construct(){
        parent::__construct();
        $this->d = D("Admin/Adtype");;
    }
    function index(){
        $action = $this->_request("action");    
        $action_array = array("show","ajax_data" , "upload_pic");
        $action = !in_array($action,$action_array)?'show':$action ;
        if($action == 'show'){
            $typeid = intval($this->_request("typeid"));
            $this->assign("typeid" , $typeid) ;
            $this->display();
        }elseif($action == 'ajax_data'){
            $this->ajax_data();
        }elseif($action == "upload_pic"){
            $this->upload_pic();
        }
    }
    //ajax get data
    private function ajax_data(){

        $page = intval($this->_request("page"  ));
        $list = $this->d->queryAdtypeList($page ,30 , 
                array(
                            'typename'=>trim($this->_request("typename")) ,
                            'status'=>trim($this->_request("status")) ,
                            
                        ) );

        echo result_to_towf_new($list['list'], 1, '成功', $list['page_string']) ;
    }
    private function upload_pic(){
        $res = $this->d->upload_adtype_pic();
        if($res['status']){
            echo result_to_towf_new(array('pic'=>$res['pic']) , 1, $res['message'], null);
            die();
        }else{
            echo result_to_towf_new($res, 0,$res['message'], null);
            die() ;
        }
    }
   
    //添加广告类别
    function add(){
        $action = $this->_request("action");        
        $action_array = array("add","doadd");
        $action = !in_array($action,$action_array)?'add':$action ;  
        if($action == 'add'){
            $this->display();       
        }elseif($action =='doadd'){
            $this->doadd();
        }
    }
    //处理添加
    private function doadd(){
        $res = $this->d->insertAdtype();
        if($res['status']){
            echo result_to_towf_new(null, 1, "success", null);
        }else{
            echo result_to_towf_new(null, 0, $res['message'], null);
            exit ;
        }   
    }
    //编辑页面
    function edit(){
        $action = $this->_request("action");        
        $action_array = array("edit","doedit" , "doStatus");
        $action = !in_array($action,$action_array)?'edit':$action ;     
        if($action == 'edit'){
            $id = intval($this->_request("id"));
            $info = $this->d->getAdtypeDataById($id);
            $this->assign("info" , $info) ;
            $this->assign("id" , $id) ;
            $this->display();       
        }elseif($action == 'doedit'){
            $this->doedit();
        }elseif($action=="doStatus"){
            $this->doStatus();
        }

    }
    //处理编辑数据
    private function doedit(){
        $res = $this->d->editAdtype();
        if($res['status']){
            echo result_to_towf_new(null, 1, $res['message'], null);
        }else{
            echo result_to_towf_new(null, 0, $res['message'], null);
            exit ;
        }
    }

    private function doStatus(){
        $data = array(
            'id'=>  intval($this->_request("id" )) , 
            'status'=>$this->_request("status" ) ,          
        );
        $res = $this->d->doStatus($data);
        echo result_to_towf_new(null, $res['status'], $res['message'], null);
    }
    //更新广告缓存
    public function update_ad_cache(){
    	$data = array(
    			'id'=>  trim($this->_request("id" )) ,
    			'type'=>  trim($this->_request("type" )) ,
    	);
    	$res = $this->d->update_ad_cache($data);
    	echo result_to_towf_new(null, $res['status'], $res['message'], null);
    }
    
}